package controller

import (
	"sync"

	"github.com/gin-gonic/gin"

	"easygo/internal/model"
	"easygo/internal/schema"
	"easygo/pkg/logger"
	"easygo/pkg/wrapper"
)

var (
	UserControllerInstance *User
	UserControllerOnce     sync.Once
)

// User Login 登录结构体
type User struct {
}

func GetUserController() *User {
	UserControllerOnce.Do(func() {
		UserControllerInstance = &User{}
	})
	return UserControllerInstance
}

func (u *User) Register(c *gin.Context) {
	ctx := c.Request.Context()

	var data schema.RegisterReqBodySchema
	if err := wrapper.ParseJSON(c, &data); err != nil {
		wrapper.ResError(c, err)
		return
	}

	res, err := model.GetUserModelInstance(ctx).Register(ctx, data)
	if err != nil {
		wrapper.ResError(c, err)
		return
	}

	ctx = logger.NewTagContext(ctx, "__register__")
	logger.WithContext(ctx).Info("注册成功")

	wrapper.ResSuccess(c, res)
}

func (u *User) Login(c *gin.Context) {
	ctx := c.Request.Context()

	var data schema.LoginReqBodySchema
	if err := wrapper.ParseJSON(c, &data); err != nil {
		wrapper.ResError(c, err)
		return
	}

	res, err := model.GetUserModelInstance(ctx).Login(ctx, data)
	if err != nil {
		wrapper.ResError(c, err)
		return
	}

	ctx = logger.NewTagContext(ctx, "__login__")
	logger.WithContext(ctx).Info("登录成功")

	wrapper.ResSuccess(c, res)
}
